﻿// Page1.cpp : 实现文件
//

#include "stdafx.h"
#include "DmToolDlg.h"
#include "Page1.h"
#include "afxdialogex.h"
#include "resource.h"
#include "CppSQLite3.h"
// CPage1 对话框
CPage1 * g_pPage1 = NULL;

#pragma region 窗体成员函数

IMPLEMENT_DYNAMIC(CPage1, CDialogEx)

CPage1::CPage1(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_PAGE1, pParent)
{

}

CPage1::~CPage1()
{
}

void CPage1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_LIST, m_lst_ocrList);
	DDX_Control(pDX, IDC_PAGE1_CMB_ACTION, m_cmb_page1_action);
	DDX_Control(pDX, IDC_CMB_PAGE1_TYPE, m_cmb_page1_type);
	DDX_Control(pDX, IDC_CMB_PAGE1_COLNUM, m_cmb_page1_colnum);
	DDX_Control(pDX, IDC_CMB_PAGE1_SIM, m_cmb_page1_sim);
	DDX_Control(pDX, IDC_CMB_PAGE1_ENLARGE, m_cmb_page1_enl);
}


BEGIN_MESSAGE_MAP(CPage1, CDialogEx)

	ON_BN_CLICKED(IDC_BTN_PAGE1_BIND, &CPage1::OnBnClickedBtnPage1Bind)
	ON_BN_CLICKED(IDC_BTN_PAGE1_MULCOL, &CPage1::OnBnClickedBtnPage1Mulcol)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_PAGE1_CMPCOL, &CPage1::OnBnClickedBtnPage1Cmpcol)
	ON_BN_CLICKED(IDC_BTN_PAGE1_GENPIC, &CPage1::OnBnClickedBtnPage1Genpic)
	ON_BN_CLICKED(IDC_BTN_PAGE1_TEST, &CPage1::OnBnClickedBtnPage1Test)
	ON_BN_CLICKED(IDC_BTN_PAGE1_CELAR, &CPage1::OnBnClickedBtnPage1Celar)
	ON_BN_CLICKED(IDC_BTN_PAGE1_SAVE, &CPage1::OnBnClickedBtnPage1Save)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST, &CPage1::OnNMDblclkList)
	ON_BN_CLICKED(IDC_BTN_PAGE1_DEL, &CPage1::OnBnClickedBtnPage1Del)
END_MESSAGE_MAP()
#pragma endregion
// CPage1 消息处理程序

#pragma region 窗口初始化
BOOL CPage1::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	int nIndex = 0;
	m_lst_ocrList.InsertColumn(nIndex++, _T("编号"), LVCFMT_LEFT, 50);
	m_lst_ocrList.InsertColumn(nIndex++, _T("名称"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("识别类型"), LVCFMT_LEFT, 80);
	m_lst_ocrList.InsertColumn(nIndex++, _T("操作类型"), LVCFMT_LEFT, 80);
	m_lst_ocrList.InsertColumn(nIndex++, _T("识别范围"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("识别数据"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("持续点击"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("偏移点击"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("输入字符"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("延时"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("查找次数"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("描述"), LVCFMT_LEFT, 100);
	m_lst_ocrList.SetBkColor(RGB(205, 226, 252));
	m_lst_ocrList.SetTextBkColor(RGB(205, 226, 252));
	m_lst_ocrList.SetTextColor(RGB(0, 0, 255));
	m_lst_ocrList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
	ReLoad();
	GetDlgItem(IDC_BTN_PAGE1_CELAR)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_DEL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_SAVE)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_TEST)->EnableWindow(SW_HIDE);
	m_cmb_page1_type.SetCurSel(0);
	m_cmb_page1_sim.SetCurSel(0);
	m_cmb_page1_colnum.SetCurSel(0);
	m_cmb_page1_action.SetCurSel(0);
	m_cmb_page1_enl.SetCurSel(0);
	//INT arr[] = { 0 };
	//VARIANT nPoint[] = { 0 };
	//SplitVARIANT("1,2,3,4", nPoint, ",");
	//TRACE("%d,%d,%d,%d", nPoint[0].lVal, nPoint[1].lVal, nPoint[2].lVal, nPoint[3].lVal);
	//addArr(arr);
	//TRACE("%d,%d,%d,%d", arr[0], arr[1], arr[2], arr[3]);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE



}
#pragma endregion


#pragma region 获取大漠工具句柄
void CPage1::GetDmToolHwnd() {
	CString sToolHwnd = m_GlobalDM->EnumWindow((long)m_ToolHwnd, L"", L"SysTabControl32", 16 + 32);
	CStringArray sHwndArr;
	SplitStr(sToolHwnd, L",", sHwndArr);
	m_OcrHwnd = iVal(sHwndArr[2]);//文字句柄 _
	SplitStr(m_GlobalDM->EnumWindow((long)m_ToolHwnd, L"", L"", 32), L",", arrDmHwnds);
	m_ColHwnds[0] = iVal(arrDmHwnds[39]);
	m_OffsetHwnds[0] = iVal(arrDmHwnds[41]);
	m_ColInfoHwnd = iVal(arrDmHwnds[97]);
	int l = 52, a = 1, p = 54, j = 0, k = 0;

	for (int i = 0; i < arrDmHwnds.GetCount(); i++)
	{

		if (i >= 118 && i <= 128 && i != 119)
		{
			m_OffsetHwnds[j] = iVal(arrDmHwnds[i]);
			j++;
			//Console.WriteLine(i + "," + dmHwnds[i]);
		}
		if (i >= 87 && i <= 96)
		{
			m_CheckHwnds[k] = iVal(arrDmHwnds[i]);
			k++;
		}
	}
	for (int i = 0; i < 9; i++)
	{
		m_ColHwnds[a] = iVal(arrDmHwnds[l]);
		m_OffsetHwnds[a] = iVal(arrDmHwnds[p]);
		p += 4;
		l += 4;
		a++;
	}

}

#pragma endregion

#pragma region 刷新
void CPage1::ReLoad()
{
	m_lst_ocrList.DeleteAllItems();

	m_cmb_page1_action.ResetContent();
	for (int i = 0; i < 5; i++)
	{
		m_cmb_page1_action.AddString(arrsAction[i]);
		//int nSize = m_cmb_page1_action.InsertString(arrsAction[i]);			
	}
	m_cmb_page1_type.ResetContent();
	for (int i = 0; i < 4; i++)
	{
		m_cmb_page1_type.AddString(arrsType[i]);
	}
	CppSQLite3DB db;
	CFileFind fFind;

	if (fFind.FindFile(_T("C:\\MobileGame\\DMTool.db")))
	{
		db.open("C:\\MobileGame\\DMTool.db","ppo00123");
		CppSQLite3Query query = db.execQuery("SELECT * FROM ocrDetail where status='0' order by id desc");
		//ocrCmp.RemoveAll();
		ocrMap.clear();
		while (!query.eof())
		{
			OcrDetail ocrDetail;
			ocrDetail.sName = query.fieldValue("name");
			ocrDetail.sOcrText = query.fieldValue("ocrText");
			ocrDetail.sAction = query.fieldValue("action");
			ocrDetail.nCount = atoi(query.fieldValue("count") == NULL ? 0 : query.fieldValue("count"));
			ocrDetail.nDelay = atoi(query.fieldValue("delay") == NULL ? 0 : query.fieldValue("delay"));
			ocrDetail.sEnlarge = query.fieldValue("enlarge");
			ocrDetail.sInStr = query.fieldValue("instr");
			ocrDetail.sOffset = query.fieldValue("offset");
			ocrDetail.sPoint = query.fieldValue("point");
			ocrDetail.sType = query.fieldValue("type");
			
			ocrMap[Str(query.fieldValue("name"))] = ocrDetail;
			//ocrMap.insert(query.fieldValue("name"), (OcrDetail*)ocrDetail);
			int nIndex = m_lst_ocrList.GetItemCount();
			int nIndex1 = 1;
			m_lst_ocrList.InsertItem(nIndex, Str(query.fieldValue("id")));
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, ocrDetail.sName);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, arrsAction[atoi(query.fieldValue("action"))]);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, arrsType[atoi(query.fieldValue("type"))]);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, ocrDetail.sPoint);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, ocrDetail.sOcrText);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, ocrDetail.sEnlarge);
			m_lst_ocrList.SetItemText(nIndex, nIndex1++, ocrDetail.sOffset);
			//m_lst_ocrList.SetItemText(nIndex, nIndex1++, CString(query.fieldValue("describe")));
			//m_lst_ocrList.SetItemData(nIndex, (DWORD)&ocrDetail);
			query.nextRow();

		}
		db.close();
	}

}
#pragma endregion

#pragma region 窗体绑定
void CPage1::OnBnClickedBtnPage1Bind()
{

	m_ToolHwnd = m_GlobalDM->FindWindow(L"", L"大漠综合工具 -  Verson:7.1822  欢迎您使用本工具，如果有问题或好的建议，请联系我");
	if (m_ToolHwnd <= 0) {
		AfxMessageBox(L"未找到大漠工具请重新打开");
		return;
	}
	m_SimHwnd = m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
	MessageBox(Str(m_SimHwnd));
	//m_SimHwnd = m_GlobalDM->GetWindow(m_SimHwnd, 1);
	//CString emHwnd = m_GlobalDM->EnumWindow(m_SimHwnd, "", "AtlAxWin90", 2);
	//m_SimHwnd = lVal(emHwnd);
	if (m_SimHwnd <= 0) {
		AfxMessageBox(L"未找到模拟器请重新打开模拟器");
		return;
	}

	int ret = g_pDmTool->BindWindowEx(m_ToolHwnd, L"dx2", L"windows", L"windows", L"", 0);
	//if (ret != 1) {
	//	AfxMessageBox("绑定大漠工具失败");
	//	return;
	//}
	CWnd* pWnd = GetDlgItem(IDC_STATICPIC);
	TRACE("%d", pWnd->m_hWnd);
	ret = m_GlobalDM->BindWindow((long)pWnd->m_hWnd, L"normal", L"windows3", L"windows", 0);
	m_GlobalDM->SetPath(L"d:\\dmpic\\");
	g_pDmLD->SetPath(L"d:\\dmpic\\");
	ret = g_pDmLD->BindWindowEx(m_SimHwnd, L"gdi", L"normal", L"windows",L"", 0);
	if (ret != 1) {
		AfxMessageBox(L"绑定雷电工具失败");
		return;
	}
	GetDmToolHwnd();
	GetDlgItem(IDC_BTN_PAGE1_CELAR)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_DEL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_SAVE)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_TEST)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_BIND)->EnableWindow(SW_HIDE);
	//g_pDmLD->FindPic(113, 15, 140, 33, L"test.bmp", L"202020", 0.9, 0, &_x, &_y);

	SetTimer(1, 1000, NULL);
	// TODO: 在此添加控件通知处理程序代码
}

#pragma endregion

#pragma region 生成多点找色
void CPage1::OnBnClickedBtnPage1Mulcol()
{
	// TODO: 在此添加控件通知处理程序代码
	m_cmb_page1_action.SetCurSel(2);
	GenCols(1);
}
#pragma endregion

#pragma region 生成颜色函数

void CPage1::GenCols(int type) {
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_HIDE);
	CString sPoints;
	sPoints.Format(L"%d,%d,%d,%d", range.x, range.y, range.x1, range.y1);
	SetDlgItemText(IDC_EDT_PAGE1_POINT, sPoints);
	int nWidth, nHeight;
	nWidth = range.x1 - range.x;
	nHeight = range.y1 - range.y;
	if (nWidth == 0 || nHeight == 0) {
		AfxMessageBox(L"请先选择图片范围");
		return;
	}
	BOOL bl = ((CButton*)GetDlgItem(IDC_CHK_PAGE1_BACK))->GetCheck();
	CStringArray arrStr;
	CString cmpColor = g_pDmTool->GetWindowTitle(m_ColHwnds[0]);
	CString sSim;
	m_cmb_page1_sim.GetWindowTextW(sSim);
	if (sSim == "") {
		AfxMessageBox(L"请选择一个相似度");
		return;
	}
	float fSim = lVal(sSim) / 100;
	for (int i = 0; i < nWidth; i++)
	{
		for (int j = 0; j < nHeight; j++)
		{
			CString s = Str(range.x + i) + "," + Str(range.y + j);
			if (bl) {//是背景图
				if (g_pDmLD->CmpColor(range.x + i, range.y + j, cmpColor, fSim) == 0)
				{
					arrStr.Add(s);
				}
			}
			else {
				arrStr.Add(s);
			}

		}

	}
	if (arrStr.GetCount() < 20) {
		AfxMessageBox(L"请选择背景");
		return;
	}
	srand((unsigned)time(NULL));
	CList<int> s;
	for (int i = 0; i < arrStr.GetCount(); i++) {
		int r = rand() % arrStr.GetCount();
		if (!s.Find(r)) {
			s.AddHead(r);
		}
		else {
			continue;
		}
	}
	CString sColNum;

	((CComboBox*)GetDlgItem(IDC_CMB_PAGE1_COLNUM))->GetWindowTextW(sColNum);
	if (sColNum == L"") {
		AfxMessageBox(L"请选择一个颜色数量");
		return;
	}
	int j = 0;
	int nColNum = iVal(sColNum);
	int* z = new int[nColNum];
	POSITION pos = s.GetHeadPosition();
	while (j<s.GetCount())
	{
		z[j++] = s.GetAt(pos);
		s.GetNext(pos);

		if (j >= nColNum)
			break;
	}
	CString str = arrStr[z[0]];
	int* arrX = new int[nColNum];
	int* arrY = new int[nColNum];
	CString* arrCols = new CString[nColNum];
	CString sY;
	int x = iVal(str.Left(str.Find(L",")));
	sY = str.Mid(str.Find(L",") + 1, str.GetLength());
	int y = iVal(sY);
	arrX[0] = x;
	arrY[0] = y;
	arrCols[0] = g_pDmLD->GetColor(x, y);
	CString sCol;
	if (type == 1) {
		sCol = arrCols[0] + L",";
	}
	CString txt = Str(x) + L"," + Str(y) + arrCols[0] + L"|";

	for (int i = 1; i < nColNum; i++)
	{
		CString str1 = arrStr[z[i]];
		int x1 = iVal(str1.Left(str1.Find(L",")));
		int y1 = iVal(str1.Mid(str1.Find(L",") + 1, str1.GetLength()));
		arrX[i] = x1;
		arrY[i] = y1;
		arrCols[i] = g_pDmLD->GetColor(x1, y1);
		if (type == 0) {
			sCol += str1 + L"," + arrCols[i] + L"|";
		}
		else {
			sCol += Str(x1 - x) + L"|" + Str(y1 - y) + L"|" + arrCols[i] + L",";
			txt += str1 + L"," + arrCols[i] + L"|";
		}


	}
	TRACE(txt);
	sCol = sCol.Mid(0, sCol.GetLength() - 1);

	SetDlgItemText(IDC_EDT_PAGE1_COL, sCol);



	//生成颜色数据

	CString color = g_pDmTool->GetWindowTitle(m_ColHwnds[0]);

	CRect  rect;
	CWnd* pWnd = GetDlgItem(IDC_STATICPIC);
	rect.left = 20;
	rect.top = 20;
	pWnd->GetClientRect(&rect);

	CDC* pDC = pWnd->GetDC();
	for (int i = 0; i < nWidth; i++)
	{
		for (int j = 0; j < nHeight; j++)
		{
			bool flag = false;
			DWORD r, g, b;
			CString sCol;
			for (int o = 0; o < 19; o++)
			{
				if (range.x + i == arrX[o] && range.y + j == arrY[o]) {
					sCol = arrCols[o];
					flag = true;
				}
			}
			if (!flag) {
				_stscanf(L"cc00cc", L"%2X%2X%2X", &r, &g, &b);
				COLORREF rgb = RGB(r, g, b);
				pDC->SetPixel(i, j, rgb);
			}
			else {
				_stscanf(sCol, L"%2X%2X%2X", &r, &g, &b);
				COLORREF rgb = RGB(r, g, b);
				pDC->SetPixel(i, j, rgb);
			}

		}
	}
	delete[] arrX;
	delete[] arrY;
	delete[] z;
	delete[] arrCols;
	OnBnClickedBtnPage1Test();
	CString strPoints;
	GetDlgItemText(IDC_STT_PAGE1_POINT, strPoints);
	while (strPoints == L"" || strPoints == L"-1,-1") {
		m_GlobalDM->delay(1500);
		GenCols('1');
		GetDlgItemText(IDC_STT_PAGE1_POINT, strPoints);
		OnBnClickedBtnPage1Test();
	}
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_SHOW);
}


#pragma endregion


#pragma region 计时事件
void CPage1::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CString sPoint;
	GetDlgItemText(IDC_EDT_PAGE1_RANGE, sPoint);
	CString sPoint1 = g_pDmTool->GetWindowTitle(iVal(arrDmHwnds[119]));
	if (sPoint != sPoint1) {
		if (sPoint1 == L"")
			return;
		SetDlgItemText(IDC_EDT_PAGE1_RANGE, sPoint1);
		CStringArray arr;
		SplitStr(sPoint1, L",", arr);
		range.x = iVal(arr[0]);
		range.y = iVal(arr[1]);
		range.x1 = iVal(arr[2]);
		range.y1 = iVal(arr[3]);
	}
	CDialogEx::OnTimer(nIDEvent);
}

#pragma endregion


#pragma region 生成比色
void CPage1::OnBnClickedBtnPage1Cmpcol()
{
	// TODO: 在此添加控件通知处理程序代码
	m_cmb_page1_action.SetCurSel(0);
	GenCols(0);
}
#pragma endregion


#pragma region 生成图片

void CPage1::OnBnClickedBtnPage1Genpic()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	//GetDlgItem(IDC_STATIC)->GetDC()
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_HIDE);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_HIDE);
	CString sPoints;
	//GetDlgItemText(IDC_TXTPOINT, str);
	sPoints.Format(L"%d,%d,%d,%d", range.x, range.y, range.x1, range.y1);
	SetDlgItemText(IDC_EDT_PAGE1_POINT, sPoints);
	CString str;
	GetDlgItemText(IDC_EDT_PAGE1_NAME, str);
	m_cmb_page1_action.SetCurSel(3);
	BOOL bl = ((CButton*)GetDlgItem(IDC_CHK_PAGE1_BACK))->GetCheck();
	CString color = g_pDmTool->GetWindowTitle(m_ColHwnds[0]);

	CRect  rect;
	CWnd* pWnd = GetDlgItem(IDC_STATICPIC);

	rect.left = 20;
	rect.top = 20;
	pWnd->GetClientRect(&rect);

	CDC* pDC = pWnd->GetDC();
	for (int i = 0; i < rect.right - rect.left; i++)
	{
		DWORD r, g, b;
		_stscanf(L"f0f0f0", L"%2X%2X%2X", &r, &g, &b);
		COLORREF rgb = RGB(r, g, b);
		for (int j = 0; j < rect.bottom - rect.top; j++)
		{
			pDC->SetPixel(i, j, rgb);
		}
	}
	//g_pDmLD->GetColor()

	for (int i = 0; i < range.x1 - range.x; i++)
	{
		for (int j = 0; j < range.y1 - range.y; j++)
		{

			DWORD r, g, b;
			if (!bl) {
				if ((i == 0 && j == 0) || (i == 0 && j == range.y1 - range.y - 1) ||
					(j == 0 && i == range.x1 - range.x - 1) || (i == range.x1 - range.x - 1 && j == range.y1 - range.y - 1))
				{
					_stscanf(L"cc00cc", L"%2X%2X%2X", &r, &g, &b);
				}
				else {
					_stscanf(g_pDmLD->GetColor(range.x + i, range.y + j), L"%2X%2X%2X", &r, &g, &b);
				}
			}
			else {
				if (g_pDmLD->CmpColor(range.x + i, range.y + j, color, 0.95f) == 0) {
					_stscanf(g_pDmLD->GetColor(range.x + i, range.y + j), L"%2X%2X%2X", &r, &g, &b);
				}
				else {
					_stscanf(L"cc00cc", L"%2X%2X%2X", &r, &g, &b);
				}
			}
			COLORREF rgb = RGB(r, g, b);
			pDC->SetPixel(i, j, rgb);
		}
	}
	//m_GlobalDM->Capture(0, 0, range.x1 - range.x, range.y1 - range.y, "test.bmp");
	if (sTimeBmp != L"") {
		str.Format(L"d:\\dmpic\\%s.bmp", sTimeBmp);
		DeleteFile(str);
		//g_pDmLD->DeleteFile(sTimeBmp);
	}
	time_t timep;
	time(&timep);
	sTimeBmp.Format(L"%d", timep);

	str.Format(L"%s%d.bmp", L"d:\\dmpic\\", timep);
	CImage Image;
	Image.Create(range.x1 - range.x, range.y1 - range.y, 24);
	::BitBlt(Image.GetDC(), 0, 0, range.x1 - range.x, range.y1 - range.y, pDC->m_hDC, 0, 0, SRCCOPY);
	Image.Save(str);
	Image.ReleaseDC();
	Image.Destroy();


	g_pDmLD->delay(2000);
	OnBnClickedBtnPage1Test();
	INT a = 1028;
	GetDlgItem(IDC_BTN_PAGE1_CMPCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_MULCOL)->EnableWindow(SW_SHOW);
	GetDlgItem(IDC_BTN_PAGE1_GENPIC)->EnableWindow(SW_SHOW);
	m_picState = true;
}

#pragma endregion


#pragma region 测试

void CPage1::OnBnClickedBtnPage1Test()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	if (range.x <= 0) {
		AfxMessageBox(L"请先生成数据!");
		return;
	}

	long lx = (long)range.x;
	long ly = (long)range.y;
	long lx1 = (long)range.x1;
	long ly1 = (long)range.y1;
	CString str;
	VARIANT _x, _y;

	GetDlgItemText(IDC_EDT_PAGE1_COL, str);
	if (m_cmb_page1_action.GetCurSel() == 0) {
		CStringArray arrStr;
		SplitStr(str, L"|", arrStr);
		BOOL bl = false;
		for (int i = 0; i < arrStr.GetCount(); i++)
		{
			CStringArray arrStr1;
			SplitStr(arrStr[i], L",", arrStr1);
			long nx = lVal(arrStr1[0]);
			long ny = lVal(arrStr1[1]);
			CString sCol = arrStr1[2];
			if (g_pDmLD->CmpColor(lVal(arrStr1[0]), lVal(arrStr1[1]), arrStr1[2], 0.90) == 1) { //未匹配
				bl = true;
			}
			else {
				if (i == 0) {
					_x.lVal = lVal(arrStr1[0]);
					_y.lVal = lVal(arrStr1[1]);
				}

			}
		}
		if (!bl) {
			SetDlgItemText(IDC_STT_PAGE1_POINT, Str(lx) + L"," + Str(ly));
		}
	}
	else if (m_cmb_page1_action.GetCurSel() == 1) {

	}
	else if (m_cmb_page1_action.GetCurSel() == 2) {
		CString str;
		GetDlgItemText(IDC_EDT_PAGE1_COL, str);
		int nIndex = str.Find(L",");
		CString str1 = str.Mid(0, nIndex);
		CString str2 = str.Mid(nIndex + 2, str.GetLength());

		g_pDmLD->FindMultiColor(lx, ly, lx1, ly1, str1, str2, 0.95, 0, &_x, &_y);

	}
	else if (m_cmb_page1_action.GetCurSel() == 3) {
		CString pic_info = L"";
		//pic_info = g_pDmLD->AppendPicAddr(pic_info, m_picData.lVal, m_picSize.lVal);
		//g_pDmLD->FindPicMem(lx - 5, ly - 5, lx1 + 5, ly1 + 5, pic_info, "202020", 0.90, 0, &_x, &_y);

		if (m_ocrDetailID != L"") {
			GetDlgItemText(IDC_EDT_PAGE1_NAME, str);
			g_pDmLD->FindPic(lx - 5, ly - 5, lx1 + 5, ly1 + 5, str + L".bmp", L"202020", 0.90, 0, &_x, &_y);
		}
		else {
			g_pDmLD->FindPic(lx - 5, ly - 5, lx1 + 5, ly1 + 5, sTimeBmp + L".bmp", L"202020", 0.90, 0, &_x, &_y);
		}
		
	}
	else if (m_cmb_page1_action.GetCurSel() == 4) {

	}
	SetDlgItemText(IDC_STT_PAGE1_POINT, Str(_x.lVal) + L"," + Str(_y.lVal));
}

#pragma endregion

#pragma region 清空

#pragma endregion

void CPage1::OnBnClickedBtnPage1Celar()
{
	// TODO: 在此添加控件通知处理程序代码
	SetDlgItemText(IDC_EDT_PAGE1_COL, L"");
	SetDlgItemText(IDC_EDT_PAGE1_DELAY, L"");
	SetDlgItemText(IDC_EDT_PAGE1_DESCRIBE,L"");
	SetDlgItemText(IDC_EDT_PAGE1_ENLARGE, L"");
	SetDlgItemText(IDC_EDT_PAGE1_INTEXT, L"");
	SetDlgItemText(IDC_EDT_PAGE1_NUM, L"");
	SetDlgItemText(IDC_EDT_PAGE1_POINT,L"");
	SetDlgItemText(IDC_EDT_PAGE1_RANGE,L"");
	SetDlgItemText(IDC_EDT_PAGE1_NAME, L"");
	SetDlgItemText(IDC_EDT_PAGE1_OFFSET, L"");
	m_ocrDetailID = "";


}

#pragma region 保存数据
void CPage1::OnBnClickedBtnPage1Save()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	GetDlgItemText(IDC_EDT_PAGE1_NAME, str);
	if (str == L"") {
		AfxMessageBox(L"请输入名称");
		return;
	}
	CppSQLite3DB db;
	CFileFind fFind;
	CString strInfo;
	CString strVal;
	try
	{
		db.open("C:\\MobileGame\\DMTool.db", "ppo00123");
		CString strSql;
		if (m_ocrDetailID == L"") {
			strSql.Format(L"select name from ocrDetail where name='%s'", str);
			CString sResult = db.execQueryOne(StrP(strSql));
			if (sResult != L"" && sResult.GetLength() > 0) {
				AfxMessageBox(L"输入的名称重复!");
				return;
			}

			strVal = L"('" + str + L"'";
			GetDlgItemText(IDC_EDT_PAGE1_COL, str);
			strVal += L",'" + str + L"','";
			strVal += Str(m_cmb_page1_action.GetCurSel()) + L"','";
			strVal += Str(m_cmb_page1_type.GetCurSel()) + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_POINT, str);
			strVal += str + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_OFFSET, str);
			strVal += str + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_ENLARGE, str);
			strVal += str + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_DELAY, str);
			strVal += str + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_INTEXT, str);
			strVal += str + L"','";
			GetDlgItemText(IDC_EDT_PAGE1_NUM, str);
			strVal += str + L"',0,'";
			GetDlgItemText(IDC_EDT_PAGE1_DESCRIBE, str);
			strVal += str + L"')";
			strSql.Format(L"INSERT INTO ocrDetail(name,ocrText,action,type,point,offset,enlarge,delay,instr,count,status,describe) VALUES%s", strVal);
		}
		else {
			strVal = L"set name='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_COL, str);
			strVal += L"ocrText='" + str + L"',";
			strVal += L" action='" + Str(m_cmb_page1_action.GetCurSel()) + L"',";
			strVal += L"type='" + Str(m_cmb_page1_type.GetCurSel()) + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_POINT, str);
			strVal += L"point='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_OFFSET, str);
			strVal += L"offset='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_ENLARGE, str);
			strVal += L"enlarge='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_DELAY, str);
			strVal += L"delay='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_INTEXT, str);
			strVal += L"instr='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_NUM, str);
			strVal += L"count='" + str + L"',";
			GetDlgItemText(IDC_EDT_PAGE1_DESCRIBE, str);
			strVal += L"describe='" + str + L"'";
			
			strVal += L" where id=" + m_ocrDetailID;
			strSql.Format(L"UPDATE ocrdetail %s", strVal);
		}
		int nAffected = db.execDML(StrP(strSql));
		GetDlgItemText(IDC_EDT_PAGE1_NAME, str);
		if (m_picState) {
			CString bmp;
			bmp.Format(L"d:\\dmpic\\%s.bmp", str);
			CWnd* pWnd = GetDlgItem(IDC_STATICPIC);
			CDC* pDC = pWnd->GetDC();
			CImage Image;
			Image.Create(range.x1 - range.x, range.y1 - range.y, 24);
			::BitBlt(Image.GetDC(), 0, 0, range.x1 - range.x, range.y1 - range.y, pDC->m_hDC, 0, 0, SRCCOPY);
			Image.Save(bmp);
			Image.ReleaseDC();
			Image.Destroy();
			m_picState = false;
		}

		OnBnClickedBtnPage1Celar();
		db.close();
		ReLoad();
	}
	catch (const std::exception&e)
	{
		db.close();
	}
	//CopyFile("c:\\log.txt", "d:\\log.txt", false);

}

#pragma endregion



void CPage1::OnNMDblclkList(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nIndex = m_lst_ocrList.GetSelectionMark();
	if (nIndex < 0) {
		return;
	}
	m_ocrDetailID = m_lst_ocrList.GetItemText(nIndex, 0);
	SetDlgItemText(IDC_EDT_PAGE1_NAME, m_lst_ocrList.GetItemText(nIndex, 1));
	SetDlgItemText(IDC_EDT_PAGE1_POINT, m_lst_ocrList.GetItemText(nIndex, 4));
	SetDlgItemText(IDC_EDT_PAGE1_COL, m_lst_ocrList.GetItemText(nIndex, 5));
	SetDlgItemText(IDC_EDT_PAGE1_ENLARGE, m_lst_ocrList.GetItemText(nIndex, 6));
	SetDlgItemText(IDC_EDT_PAGE1_OFFSET, m_lst_ocrList.GetItemText(nIndex, 7));
	SetDlgItemText(IDC_EDT_PAGE1_INTEXT, m_lst_ocrList.GetItemText(nIndex, 8));
	//SetDlgItemText(IDC_EDT_PAGE1_DESCRIBE, m_lst_ocrList.GetItemText(nIndex, 0));
	SetDlgItemText(IDC_EDT_PAGE1_DELAY, m_lst_ocrList.GetItemText(nIndex, 9));
	SetDlgItemText(IDC_EDT_PAGE1_NUM, m_lst_ocrList.GetItemText(nIndex, 10));
	//OcrDetail* ocrDetail = (OcrDetail*)m_lst_ocrList.GetItemData(nIndex);
	OcrDetail ocrDetail = ocrMap[m_lst_ocrList.GetItemText(nIndex, 1)];
	m_cmb_page1_action.SetCurSel(iVal(ocrDetail.sAction));
	m_cmb_page1_type.SetCurSel(iVal(ocrDetail.sType));
	*pResult = 0;
}


void CPage1::OnBnClickedBtnPage1Del()
{
	// TODO: 在此添加控件通知处理程序代码
}
